package com.kevin.springboot.demos.utils;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.BlockElement;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.font.FontInfo;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.layout.property.TextAlignment;
import com.kevin.springboot.demos.handler.PdfFooterEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Optional;

/**
 * html转换pdf
 * @author: wangyong
 * @date: 2019/8/17 14:39
 */
public class HtmlConvertPdfUtil {

	private static final Logger logger = LoggerFactory.getLogger(HtmlConvertPdfUtil.class);

	public static void convert(String html, OutputStream outputStream,String title) throws IOException {
//		PdfWriter writer = new PdfWriter(outputStream);
//		PdfDocument pdfDocument = new PdfDocument(writer);
//		pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PdfFooterEvent());
//		try {
//			ConverterProperties props = new ConverterProperties();
//			FontProvider fp = new FontProvider();
//			fp.addStandardPdfFonts();
//		//	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//			fp.addDirectory(new ClassPathResource("/static/fonts/").getPath());
//			props.setFontProvider(fp);
//			List<IElement> iElements = HtmlConverter.convertToElements(html, props);
//			Document document = new Document(pdfDocument, PageSize.A4, true); // immediateFlush设置true和false都可以，false 可以使用 relayout
//			//document.setMargins(topMargin, rightMargin, bottomMargin, leftMargin);
//			for (IElement iElement : iElements) {
//				BlockElement blockElement = (BlockElement) iElement;
//				blockElement.setMargins(1, 0, 1, 0);
//				document.add(blockElement);
//			}
//			document.close();
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			outputStream.close();
//		}



















		Document document = null;
		try {
			ConverterProperties proper = new ConverterProperties();
			proper.setFontProvider(getFont());
			//convertToPdf(html,outputStream,proper);
			PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputStream));
			pdfDocument.setDefaultPageSize(PageSize.A4.rotate());
			pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PdfFooterEvent());
			PdfDocumentInfo documentInfo = pdfDocument.getDocumentInfo();
			documentInfo.setTitle(title);
			document = new Document(pdfDocument);
			document.setMargins(20, 12, 20, 12);
			List<IElement> list = HtmlConverter.convertToElements(html, proper);
			for (IElement ie : list) {
				document.add((IBlockElement) ie);
			}
		} catch (Exception e) {
			logger.error("转换pdf出现异常",e);
		}finally {
			assert document != null;
			document.close();
		}
	}

	/**
	 * 字体
	 */
	static FontProvider getFont(){
		FontProvider font = new FontProvider();
		font.addStandardPdfFonts();
		font.addDirectory(new ClassPathResource("/static/fonts/").getPath());
		font.addFont("/static/fonts/STHeitibd.ttf");
		return font;
	}
}
